package worker_tools

import (
	"GraduationProject/public/db"
	"GraduationProject/public/models"
	"crypto/md5"
	"encoding/hex"
	"github.com/go-redis/redis"
	"strconv"
)

const (
	header = "duplicate_removal_"
)

// todo: 可能需要将params反序列化并进行排序
func _md5(url, params string) string {
	md5Obj := md5.New()
	md5Obj.Write([]byte(url + params))
	return hex.EncodeToString(md5Obj.Sum(nil))
}

func DuplicateRemoval(seed models.Record) (exist bool) {
	if _, err := db.Redis.HGet(
		header+strconv.Itoa(int(seed.SeedID)),
		_md5(seed.Url, seed.Params),
	).Result(); err == redis.Nil {
		return false
	}
	return true
}

func Visited(seed models.Record) {
	db.Redis.HSet(
		header+strconv.Itoa(int(seed.SeedID)),
		_md5(seed.Url, seed.Params), true)
}
